<?php

namespace app\admin\controller;


use app\common\traites\PublicCrudTrait;
use app\common\tencent\TLSSigAPIv2;
use app\BaseController;


class Tencent extends Base
// class Tencent extends BaseController
{
    use PublicCrudTrait;

    // 固定的
    // public $host = "console.tim.qq.com";  // 中国域名  
    public $host = "adminapisgp.im.qcloud.com"; // 新加坡域名
    public $http = "https";
    public $ver = "v4";
    public $sdkappid = "";
    public $identifier = "";
    public $usersig = "";
    public $contenttype = 'json';
    public $random = ''; 
    // 每次请求变动
    public $servicename = "";
    public  $command = "";

     /**
     *配置控制器
     */
    public function initialize()
    {
        $tencent_adminuser = $this->app->config->get('app.tencent_adminuser');
        $tencent_sdkapiid = $this->app->config->get('app.tencent_sdkapiid');
        $tencent_key =$this->app->config->get('app.tencent_key');

        $this->sdkappid = $tencent_sdkapiid;
        $this->identifier = $tencent_adminuser;

        // 本地生成 签名的工具
        $api = new TLSSigAPIv2( $tencent_sdkapiid, $tencent_key);
        $sig = $api->genUserSig($tencent_adminuser);
        $this->usersig = $sig;
        // 调用 父级的
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function makeUrl(){
        $this->random = rand(172060507,972060507);
        $str = "{$this->http}://{$this->host}/{$this->ver}/{$this->servicename}/{$this->command}?sdkappid={$this->sdkappid}&identifier={$this->identifier}&usersig={$this->usersig}&random={$this->random}&contenttype={$this->contenttype}";
        return $str;
    }

    /**
     * 列表
     */
    public function index()
    {   
        // 返回 展示数据
        $showData = array();
    
        // 获取 全部的群
        $this->servicename = "group_open_http_svc";
        $this->command = "get_appid_group_list";
        $posturl = $this->makeUrl();
        $postData = array();
        $postData['name'] = ''; // 这里就是 为了 传递一个 废弃的参数 
        $psotJsonData = json_encode($postData);
        $res = curl_request_tencent($posturl,'POST',$psotJsonData,'https');
        $resData = json_decode($res);

        if($resData->ActionStatus == "OK"){
            foreach ($resData->GroupIdList as $k => $v){
                $temp = array();
                $temp["ID"] = $v->GroupId;
                $temp["groupName"] = $this->getGroupNma($v->GroupId);
                $showData[] = $temp;
            }
        }
        return $this->success($showData);
    }
    // 暂时没用了
    // 内部 循环 获取 群组名称
    public function getGroupNma($groupID){

        // 获取 全部的群
        $this->servicename = "group_open_http_svc";
        $this->command = "get_group_info";
        $posturl = $this->makeUrl();
        $postData = array('GroupIdList'=>array($groupID));
        $psotJsonData = json_encode($postData);
        $res = curl_request_tencent($posturl,'POST',$psotJsonData,'https');
        $resData = json_decode($res);
        // var_dump($resData);
        if($resData->ActionStatus == "OK"){
            $info = $resData->GroupInfo;
            
            return $info[0]->Name;
        }
        return "未获取到群名称";
    }
    
    // 获取群组 详细信息
    public function getGroupDetail(){
        //过滤数据
        $postField = 'groupID';
        $post = $this->request->only(explode(',', $postField), 'post', null);
        
        // 返回 展示数据
        $showData = array();
    
        // 获取 全部的群
        $this->servicename = "group_open_http_svc";
        $this->command = "get_group_info";
        $posturl = $this->makeUrl();
        $postData = array('GroupIdList'=>array($post['groupID']));
        // var_dump($postData);
        // $postData->GroupIdList = ; // 这里就是 为了 传递一个 废弃的参数 
        $psotJsonData = json_encode($postData);
        // echo $psotJsonData;
        // return;
        $res = curl_request_tencent($posturl,'POST',$psotJsonData,'https');
        $resData = json_decode($res);
        // var_dump($posturl);
        // var_dump($res);
        // var_dump($resData);
        if($resData->ActionStatus == "OK"){
            $info = $resData->GroupInfo;
            // var_dump($info[0]->Name);
            $showData['name'] = $info[0]->Name;
        }
        return show($showData);
    }

    /**
     * 解散
     * @return mixed
     */
    public function disband()
    {
        //过滤数据
        $postField = 'groupID';
        $post = $this->request->only(explode(',', $postField), 'post', null);
        
        // 返回 展示数据
        $showData = array();
    
        // 获取 全部的群
        $this->servicename = "group_open_http_svc";
        $this->command = "destroy_group";
        $posturl = $this->makeUrl();
        $postData = array('GroupId'=>$post['groupID']);
        // var_dump($postData);
        // $postData->GroupIdList = ; // 这里就是 为了 传递一个 废弃的参数 
        $psotJsonData = json_encode($postData);
        // echo $psotJsonData;
        // return;
        $res = curl_request_tencent($posturl,'POST',$psotJsonData,'https');
        $resData = json_decode($res);
        // var_dump($posturl);
        // var_dump($res);
        // var_dump($resData);
        $showData['OK'] = "FAIL";
        if($resData->ActionStatus == "OK"){
            $info = $resData->GroupInfo;
            // var_dump($info[0]->Name);
            $showData['OK'] = "OK";
        }
        return show($showData);
    }
    
    /**
     * 获取 历史消息
     * @return mixed
     */
    public function getGroupHistoryMsg()
    {
        //过滤数据
        $postField = 'groupID';
        $post = $this->request->only(explode(',', $postField), 'post', null);
        
        // 返回 展示数据
        $showData = array();
    
        // 获取 全部的群
        $this->servicename = "group_open_http_svc";
        $this->command = "group_msg_get_simple";
        $posturl = $this->makeUrl();
        $postData = array('GroupId'=>$post['groupID'],'ReqMsgNumber'=>20);
        // var_dump($postData);
        // $postData->GroupIdList = ; // 这里就是 为了 传递一个 废弃的参数 
        $psotJsonData = json_encode($postData);
        // echo $psotJsonData;
        // return;
        $res = curl_request_tencent($posturl,'POST',$psotJsonData,'https');
        $resData = json_decode($res);
        // var_dump($posturl);
        // var_dump($res);
        // var_dump($resData);
        // $showData['OK'] = "FAIL";
        if($resData->ActionStatus == "OK"){
            // var_dump($info[0]->Name);
            $showData['RspMsgList'] = $resData->RspMsgList;
        }
        return show($showData);
    }
    
     /**
     * 撤回消息
     * @return mixed
     */
    public function recallGroupMsg()
    {
        //过滤数据
        $postField = 'groupID,MsgSeq';
        $post = $this->request->only(explode(',', $postField), 'post', null);
        
        // 返回 展示数据
        $showData = array();
    
        // 获取 全部的群
        $this->servicename = "group_open_http_svc";
        $this->command = "group_msg_recall";
        $posturl = $this->makeUrl();
        $postData = array('GroupId'=>$post['groupID'],'MsgSeqList'=>[['MsgSeq'=>$post['MsgSeq']]]);
        // var_dump($postData);
        // $postData->GroupIdList = ; // 这里就是 为了 传递一个 废弃的参数 
        $psotJsonData = json_encode($postData);
        // echo $psotJsonData;
        // return;
        $res = curl_request_tencent($posturl,'POST',$psotJsonData,'https');
        $resData = json_decode($res);
        // var_dump($posturl);
        // var_dump($res);
        // var_dump($resData);
        $showData['OK'] = "FAIL";
        if($resData->ActionStatus == "OK"){
            $showData['OK'] = "OK";
        }
        return show($showData);
    }
    
    /**
     * 设置用户头像
     * @return mixed
     */
    public function setUserHeadImg()
    {
        //过滤数据
        $postField = 'userId,userheadImgUrl';
        $post = $this->request->only(explode(',', $postField), 'post', null);
        
        // 返回 展示数据
        $showData = array();
    
        // 获取 全部的群
        $this->servicename = "profile";
        $this->command = "portrait_set";
        $posturl = $this->makeUrl();
        $postData = array('From_Account'=>$post['userId'],'ProfileItem'=>[['Tag'=>'Tag_Profile_IM_Image','Value'=>$post['userheadImgUrl']]]);
        // var_dump($postData);
        // $postData->GroupIdList = ; // 这里就是 为了 传递一个 废弃的参数 
        $psotJsonData = json_encode($postData);
        // echo $psotJsonData;
        // return;
        $res = curl_request_tencent($posturl,'POST',$psotJsonData,'https');
        $resData = json_decode($res);
        // var_dump($posturl);
        // var_dump($res);
        // var_dump($resData);
        $showData['OK'] = "FAIL";
        if($resData->ActionStatus == "OK"){
            $showData['OK'] = "OK";
        }
        return show($showData);
    }
    
    
// 类结束了    
}
















